/*
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2023-11-21 09:31:31
 * @LastEditors: huangtianyang
 * @LastEditTime: 2024-04-07 10:42:02
 * @FilePath: \cxl-driving-training\src\views\trainingGround\edit\index.ts
 */
import { formDataInterface, PropsFormpData } from '@/utils/interface'
import { reactive, ref } from 'vue'
import { listConfig } from './formConfig'
import { useRoute } from 'vue-router'
import { routerStore } from '@/store/router.store'
import { trainSiteAdd, trainSiteDetailed, trainSiteEdit, trainSiteOperationInterface } from '@/apis/trainSite'
import { ElMessage } from 'element-plus'
import router from '@/router'
import { userStore } from '@/store/user.store'
export default class CoachOperation {
    formData = reactive<PropsFormpData[]>([...listConfig])
    //编辑类型 create | edit | look
    operation = reactive({
        type: 'create',
        id: '',
        regionId: ''
    })
    routerStore = routerStore()
    uStore = userStore()
    constructor() {
        const route = useRoute()
        this.operation.type = route.query.type as string
        this.operation.id = route.query?.id as string

        // const arr: any[] = []
        // this.uStore.drivingSchoolSimple.forEach(item => {
        //     arr.push({ label: item.shortName, value: item.id })
        // })
        // this.formData[0][3].options = arr

        if (this.operation.type === 'create') {
            //初始化数据
        } else {
            this.getData()
        }
    }
    getData = (arg?: any) => {
        trainSiteDetailed(this.operation.id).then(res => {
            if (Number(res.data.code) === 200) {
                const result = res.data.result
                this.operation.regionId = result.regionId
                // this.formData.forEach((child: formDataInterface[]) => {
                //     child.forEach((item: formDataInterface) => {
                //         if (item.key === 'name') {
                //             item.value = result.name
                //         } else if (item.key === 'siteType') {
                //             item.value = result.siteType + ''
                //         } else if (item.key === 'ownFlag') {
                //             item.value = result.ownFlag + ''
                //         } else if (item.key === 'openHours') {
                //             console.log(result.openHours)
                //             item.value = result.openHours + ''
                //             // item.value = result.openHours + ''
                //         } else if (item.key === 'equipmentIntroduction') {
                //             item.value = result.equipmentIntroduction + ''
                //         } else if (item.key === 'siteIntroduction') {
                //             item.value = result.siteIntroduction + ''
                //         } else if (item.key === 'addressDetail') {
                //             item.value = result.addressDetail + ''
                //             item.latLng = [result.lat, result.lng]
                //         } else if (item.key === 'sitePicList') {
                //             if (result.sitePicList.length) {
                //                 const arr: any[] = []
                //                 result.sitePicList.forEach((tag: string) => {
                //                     arr.push({ key: tag, tag: '', index: '' })
                //                 })

                //                 item.value = arr
                //             }
                //         } else if (item.key === 'supportFacilitys') {
                //             if (result.supportFacilitys.length) {
                //                 const arr: any[] = []
                //                 console.log(result.supportFacilitys)
                //                 result.supportFacilitys.forEach((tag: any) => {
                //                     arr.push({ key: tag.pic, tag: tag.name, index: '' })
                //                 })
                //                 item.value = arr
                //                 console.log('item.value', item.value)
                //             }
                //         }
                //     })
                // })
                console.log('this.formData', this.formData)
            }
        })
    }
    //取消创建
    onCancel = () => {
        this.routerStore.removeHeaderRoute(this.routerStore.selectIndex)
        router.push('/subjectTwo')
    }
    //保存
    onSave = (formRef: any) => {
        console.log(this.formData, formRef)

        const data: trainSiteOperationInterface = {
            sitePicList: [],
            supportFacilitys: []
        }

        if (formRef?.validateForm()) {
            // this.formData.forEach((child: formDataInterface[]) => {
            //     child.forEach((item: formDataInterface) => {
            //         if (item.key === 'addressDetail') {
            //             data.addressDetail = item.value + ''
            //             data.lat = (item.latLng as (string | number)[])[0] + ''
            //             data.lng = (item.latLng as (string | number)[])[1] + ''
            //             data.regionId = item.regionId + '' || this.operation.regionId
            //         } else if (item.key === 'name') {
            //             data.name = item.value as string
            //         } else if (item.key === 'siteType') {
            //             data.siteType = Number(item.value)
            //         } else if (item.key === 'ownFlag') {
            //             data.ownFlag = Number(item.value)
            //         } else if (item.key === 'openHours') {
            //             data.openHours = item.value as string
            //         } else if (item.key === 'sitePicList') {
            //             const arr: { key: string; tag: string }[] = item.value as any[]
            //             // arr.forEach(i => {
            //             //     data.sitePicList?.push(i.key)
            //             // })
            //             for (let i = 0; i < 6; i++) {
            //                 data.sitePicList?.push(arr[i] ? arr[i].key : '')
            //             }
            //         } else if (item.key === 'equipmentIntroduction') {
            //             data.equipmentIntroduction = item.value + ''
            //         } else if (item.key === 'siteIntroduction') {
            //             data.siteIntroduction = item.value + ''
            //         } else if (item.key === 'supportFacilitys') {
            //             console.log(item.value)
            //             const arr: { key: string; tag: string }[] = item.value as any[]
            //             // arr.forEach(i => {
            //             //     data.supportFacilitys?.push({ name: i.tag, pic: i.key })
            //             // })
            //             for (let i = 0; i < 8; i++) {
            //                 data.supportFacilitys?.push({
            //                     name: arr[i] ? arr[i].tag : '',
            //                     pic: arr[i] ? arr[i].key : ''
            //                 })
            //             }
            //         }
            //     })
            // })

            console.log('data', data)
            if (this.operation.type === 'create') {
                trainSiteAdd(data).then(res => {
                    if (Number(res.data.code) === 200) {
                        ElMessage.success('新增成功！')
                        this.routerStore.removeHeaderRoute(this.routerStore.selectIndex)
                        router.push('/subjectTwo')
                    }
                })
            } else {
                data.id = this.operation.id
                trainSiteEdit(data).then(res => {
                    if (Number(res.data.code) === 200) {
                        ElMessage.success('修改成功！')
                        this.routerStore.removeHeaderRoute(this.routerStore.selectIndex)
                        router.push('/subjectTwo')
                    }
                })
            }
        }
    }
}
